package com.ruoyi.web.controller.biz;

import com.ruoyi.biz.domain.BizUser;
import com.ruoyi.biz.service.IBizUserService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.biz.BizLoginBody;
import com.ruoyi.common.core.domain.model.biz.BizRegistBody;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.BizRegisterService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 用户信息Controller
 *
 * @author ruoyi
 * @date 2021-12-07
 */
@RestController
@RequestMapping("/biz/user")
public class BizUserController extends BaseController {
    @Autowired
    private IBizUserService bizUserService;

    @Autowired
    private BizRegisterService registerService;


    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
/*    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }*/

    /**
     * 用戶注册
     */
    /*@PostMapping("/register")
    public AjaxResult register(@RequestBody BizRegistBody user)
    {
        String msg = registerService.registerBizUser(user);
        return StringUtils.isEmpty(msg) ? success() : error(msg);
    }*/


    @RequestMapping("/register")
    public AjaxResult register(BizRegistBody userRequest) {
        BizUser bizUser = new BizUser();
        return toAjax(registerService.registerBizUser1(userRequest));
    }

    @RequestMapping("/login")
    public AjaxResult login(BizLoginBody loginBody) {
        AjaxResult ajax = AjaxResult.success();
        BizUser condition = new BizUser();
        condition.setEmail(loginBody.getEmail());
        condition.setPassword(loginBody.getPassword());
        BizUser bizUser = bizUserService.login(condition);
        ajax.put("user", bizUser);
        return ajax;
    }

    /**
     * 查询用户信息列表
     */
   // @PreAuthorize("@ss.hasPermi('system:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizUser bizUser) {
        startPage();
        List<BizUser> list = bizUserService.selectBizUserList(bizUser);
        return getDataTable(list);
    }

    /**
     * 导出用户信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:user:export')")
    @Log(title = "用户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizUser bizUser) {
        List<BizUser> list = bizUserService.selectBizUserList(bizUser);
        ExcelUtil<BizUser> util = new ExcelUtil<BizUser>(BizUser.class);
        util.exportExcel(response, list, "用户信息数据");
    }

    /**
     * 获取用户信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:user:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId) {
        return AjaxResult.success(bizUserService.selectBizUserByUserId(userId));
    }

    /**
     * 新增用户信息
     */
/*
    @PreAuthorize("@ss.hasPermi('system:user:add')")
    @Log(title = "用户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizUser bizUser)
    {
        return toAjax(bizUserService.insertBizUser(bizUser));
    }
*/

    /**
     * 修改用户信息
     */
/*    @PreAuthorize("@ss.hasPermi('system:user:edit')")
    @Log(title = "用户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizUser bizUser)
    {
        return toAjax(bizUserService.updateBizUser(bizUser));
    }*/

    /**
     * 删除用户信息
     */
/*    @PreAuthorize("@ss.hasPermi('system:user:remove')")
    @Log(title = "用户信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        return toAjax(bizUserService.deleteBizUserByUserIds(userIds));
    }*/
}
